package com.four.service.impl;

import com.four.entity.News;
import com.four.entity.Reg;
import com.four.entity.User;
import com.four.mapper.RegMapper;
import com.four.service.RegService;
import com.four.util.WebUtil;
import com.four.vo.EchartVo;
import com.four.vo.RegVo;
import com.four.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;

import javax.servlet.annotation.WebFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: WangPeng
 * @createTime: 2022/10/13 15:33
 * @description:
 * @version: v1.0
 */
@Service
public class RegServiceImpl implements RegService {

    @Autowired
    private RegMapper regMapper;

    @Override
    public Result addReg(RegVo regVo) {
        User user = (User) WebUtil.getSession().getAttribute("user");
        System.out.println(user);
        regVo.setUserId(user.getUserId());
        System.out.println(regVo);
        int i = regMapper.addReg(regVo);
        System.out.println(regVo);
        return new Result(200,"添加成功");
    }


    @Override
    public Result deleteByRegId(Integer regId) {
        int i = regMapper.deleteByRegId(regId);
        return new Result(200,"删除成功");
    }

    @Override
    public Result updateRegByRegId(Reg reg) {
        int i = regMapper.updateRegByRegId(reg);
        return new Result(200,"更改成功");
    }

    @Override
    public Result selelctRegByAdminId(Integer currentPage, Integer pageSize,Integer adminId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Reg> regs1 = regMapper.selelctRegByAdminId(adminId);
        PageInfo<Reg> pageInfo = new PageInfo<>(regs1);
        return new Result(200,"查询成功2",pageInfo);
    }

    @Override
    public Result selectByRegId(Integer regId) {
        Reg reg = regMapper.selectByRegId(regId);
        return new Result(200,"查询成功reg",reg);
    }

    /**
     * 根据患者姓名和患者身份证号查询预约信息
     */
    @Override
    public Result selectByRegNameAndIdcard(String regName, String idcard) {
        List<Reg> regs = regMapper.selectByRegNameAndIdcard(regName, idcard);
        return new Result(200,"查询成功",regs);
    }

    @Override
    public Result selectByRegNameIdcardPhonenumber(String regName, String idcard, String phonenumber) {
        List<Reg> regs = regMapper.selectByRegNameIdcardPhonenumber(regName, idcard, phonenumber);
        return new Result(200,"查询成功",regs);
    }

    @Override
    public Result sexStat() {
        List<EchartVo> sexStats = regMapper.selectBySex();
        return new Result(200,"查询男女比例成功",sexStats);
    }

    @Override
    public Result ageStat() {
        List<EchartVo> ageStats = regMapper.selectByAge();
        return new Result(200,"查询年龄分布成功",ageStats);
    }

    @Override
    public Result dateStat() {
        List<EchartVo> echartVos = regMapper.selectByDate();
        Map<String, Object> map = RegServiceImpl.staUtil(echartVos);
        return new Result(200,"查询一周预约量成功",map);
    }

    @Override
    public Result incomeStat() {
        List<EchartVo> echartVos = regMapper.selectByIncome();
        Map<String, Object> map = RegServiceImpl.staUtil(echartVos);
        return new Result(200,"查询一周营收成功",map);
    }


    private static Map<String , Object> staUtil(List<EchartVo> echartVos){
        List<String> date = new ArrayList<>();
        List<Integer> num = new ArrayList<>();
        for (EchartVo echartVo : echartVos) {
            if (!echartVo.getName().equals("other")){
                date.add(echartVo.getName());
                num.add(echartVo.getValue());
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date",date);
        map.put("num",num);
        return map;
    }


}
